package com.Offer;

import com.pojo.ListNode;

import java.util.List;

/*
    面试题24：反转链表
    输入一个链表，反转链表后，输出新链表的表头。
    测试用例：1,2,3,4,5,6
 */
public class demo24 {
    public static void main(String[] args) {

        int[] input = new int[]{1,2,3,4,5,6};
        ListNode head = new ListNode(1);
        for(int i = 1;i<input.length;i++){
            head.add(input[i]);
        }
        ListNode listNode = reverseList(head);
        while(listNode != null){
            System.out.print(listNode.val+" ");
            listNode = listNode.next;
        }
    }


    public static ListNode reverseList(ListNode head){
        ListNode prv = null;
        ListNode res = null;
        ListNode node = head;
        while(node != null){
            ListNode next = node.next;
            if(next == null){
                res = node;
            }

            node.next = prv;
            prv = node;
            node = next;
        }
        return res;
    }
}
